import random

import wx


class MyFrame(wx.Frame):
    NameList = ['tom', 'jack', 'lily']
    pos_x, pos_y = 10, 70
    btn_w, btn_h = 58, 55

    def __init__(self):
        wx.Frame.__init__(self, None, title='简单计算器', pos=(100, 100), size=(300, 440))
        # 创建面板
        self.pl = wx.Panel(self, pos=(0, 0), size=(300, 400))
        # 文本显示框
        self.entity = wx.TextCtrl(self.pl, pos=(10, 10), size=(260, 50), style=wx.TE_RIGHT)
        #
        # 第一行
        self.btn_clear = wx.Button(self.pl, label='C', pos=(self.pos_x, self.pos_y), size=(self.btn_w, self.btn_h))
        self.btn_div = wx.Button(self.pl, label='/', pos=(self.pos_x + self.btn_w + 10, self.pos_y),
                                 size=(self.btn_w, self.btn_h))
        self.btn_mul = wx.Button(self.pl, label='*', pos=(self.pos_x + (self.btn_w + 10) * 2, self.pos_y),
                                 size=(self.btn_w, self.btn_h))

        self.btn_back = wx.Button(self.pl, label='<-', pos=(self.pos_x + (self.btn_w + 10) * 3, self.pos_y),
                                  size=(self.btn_w, self.btn_h))
        # 第二行
        self.btn_7 = wx.Button(self.pl, label='7', pos=(self.pos_x, self.pos_y + self.btn_h + 10),
                               size=(self.btn_w, self.btn_h))
        self.btn_8 = wx.Button(self.pl, label='8', pos=(self.pos_x + self.btn_w + 10, self.pos_y + self.btn_h + 10),
                               size=(self.btn_w, self.btn_h))
        self.btn_9 = wx.Button(self.pl, label='9',
                               pos=(self.pos_x + (self.btn_w + 10) * 2, self.pos_y + self.btn_h + 10),
                               size=(self.btn_w, self.btn_h))
        self.btn_sub = wx.Button(self.pl, label='-',
                                 pos=(self.pos_x + (self.btn_w + 10) * 3, self.pos_y + self.btn_h + 10),
                                 size=(self.btn_w, self.btn_h))
        # 第三行
        self.btn_4 = wx.Button(self.pl, label='4', pos=(self.pos_x, self.pos_y + (self.btn_h + 10) * 2),
                               size=(self.btn_w, self.btn_h))
        self.btn_5 = wx.Button(self.pl, label='5',
                               pos=(self.pos_x + self.btn_w + 10, self.pos_y + (self.btn_h + 10) * 2),
                               size=(self.btn_w, self.btn_h))
        self.btn_6 = wx.Button(self.pl, label='6',
                               pos=(self.pos_x + (self.btn_w + 10) * 2, self.pos_y + (self.btn_h + 10) * 2),
                               size=(self.btn_w, self.btn_h))
        self.btn_add = wx.Button(self.pl, label='+',
                                 pos=(self.pos_x + (self.btn_w + 10) * 3, self.pos_y + (self.btn_h + 10) * 2),
                                 size=(self.btn_w, self.btn_h))
        # 第四行
        self.btn_1 = wx.Button(self.pl, label='1', pos=(self.pos_x, self.pos_y + (self.btn_h + 10) * 3),
                               size=(self.btn_w, self.btn_h))
        self.btn_2 = wx.Button(self.pl, label='2',
                               pos=(self.pos_x + self.btn_w + 10, self.pos_y + (self.btn_h + 10) * 3),
                               size=(self.btn_w, self.btn_h))
        self.btn_3 = wx.Button(self.pl, label='3',
                               pos=(self.pos_x + (self.btn_w + 10) * 2, self.pos_y + (self.btn_h + 10) * 3),
                               size=(self.btn_w, self.btn_h))
        self.btn_eq = wx.Button(self.pl, label='=',
                                pos=(self.pos_x + (self.btn_w + 10) * 3, self.pos_y + (self.btn_h + 10) * 3),
                                size=(self.btn_w, self.btn_h * 2 + 10))
        # 第五行
        self.btn_0 = wx.Button(self.pl, label='0', pos=(self.pos_x, self.pos_y + (self.btn_h + 10) * 4),
                               size=(self.btn_w * 2 + 10, self.btn_h))
        self.btn_dot = wx.Button(self.pl, label='*',
                                 pos=(self.pos_x + self.btn_w * 2 + 20, self.pos_y + (self.btn_h + 10) * 4),
                                 size=(self.btn_w, self.btn_h))
        # 绑定按钮
        self.Bind(wx.EVT_BUTTON, self.on_btn_clear, self.btn_clear)
        self.Bind(wx.EVT_BUTTON, self.on_btn_div, self.btn_div)
        self.Bind(wx.EVT_BUTTON, self.on_btn_mul, self.btn_mul)
        self.Bind(wx.EVT_BUTTON, self.on_btn_back, self.btn_back)
        self.Bind(wx.EVT_BUTTON, self.on_btn_7, self.btn_7)
        self.Bind(wx.EVT_BUTTON, self.on_btn_8, self.btn_8)
        self.Bind(wx.EVT_BUTTON, self.on_btn_9, self.btn_9)
        self.Bind(wx.EVT_BUTTON, self.on_btn_4, self.btn_4)
        self.Bind(wx.EVT_BUTTON, self.on_btn_5, self.btn_5)
        self.Bind(wx.EVT_BUTTON, self.on_btn_6, self.btn_6)
        self.Bind(wx.EVT_BUTTON, self.on_btn_1, self.btn_1)
        self.Bind(wx.EVT_BUTTON, self.on_btn_2, self.btn_2)
        self.Bind(wx.EVT_BUTTON, self.on_btn_3, self.btn_3)
        self.Bind(wx.EVT_BUTTON, self.on_btn_0, self.btn_0)
        self.Bind(wx.EVT_BUTTON, self.on_btn_dot, self.btn_dot)
        self.Bind(wx.EVT_BUTTON, self.on_btn_add, self.btn_add)
        self.Bind(wx.EVT_BUTTON, self.on_btn_sub, self.btn_sub)
        self.Bind(wx.EVT_BUTTON, self.on_btn_eq, self.btn_eq)

    def on_btn_7(self, event):
        self.entity.AppendText('7')

    def on_btn_8(self, event):
        self.entity.AppendText('8')

    def on_btn_9(self, event):
        self.entity.AppendText('9')

    def on_btn_4(self, event):
        self.entity.AppendText('4')

    def on_btn_5(self, event):
        self.entity.AppendText('5')

    def on_btn_6(self, event):
        self.entity.AppendText('6')

    def on_btn_1(self, event):
        self.entity.AppendText('1')

    def on_btn_2(self, event):
        self.entity.AppendText('2')

    def on_btn_3(self, event):
        self.entity.AppendText('3')

    def on_btn_0(self, event):
        self.entity.AppendText('0')

    def on_btn_dot(self, event):
        self.entity.AppendText('.')

    def on_btn_clear(self, event):
        self.entity.SetValue('')

    def on_btn_add(self, event):
        self.entity.AppendText('+')

    def on_btn_sub(self, event):
        self.entity.AppendText('-')

    def on_btn_div(self, event):
        self.entity.AppendText('/')

    def on_btn_eq(self, event):
        text = self.entity.GetValue()
        result = str(eval(text))
        print("result=",result)
        self.entity.SetValue(result)

    def on_btn_mul(self, event):
        self.entity.AppendText('*')

    def on_btn_back(self, event):
        s = self.entity.GetValue()
        self.entity.SetLabelText(s[:-1])


if __name__ == '__main__':  # python程序的主入口
    # 创建应用程序对象
    app = wx.App()
    # 创建窗口
    frm = MyFrame()
    # 显示窗口
    frm.Show()
    # 进入主循环，让窗口一直显示
    app.MainLoop()
